<?
require_once('version.inc');
require_once('dbaccess.inc');
require_once('language.inc');
require_once('header.inc');
require_once('crypt.inc');
require_once('settings.inc');
require_once('menu.inc');

function SaveNewRole() {
	$kr_num = $_REQUEST['kr_num'];
	$kr_role = $_REQUEST['kr_role'];

	$db = OpenDB();
	$query = "select kr_role from key_role where kr_num = $kr_num";
	$result = QueryDB($db, $query);
	$numrows = numrowsDB($result);

	if ($numrows == 1) {
	   Error("Schl&uuml;ssel $kr_num ist bereits vorhanden!\n");
	   closeDB($db);
	   return false;
	}

	$query = "insert into key_role (kr_num, kr_role) values ($kr_num, '$kr_role')";

	if (!TQueryDB($db, $query)) {
	   Error("SQL-Error: $query");
	   closeDB($db);
	   return false;
	}

	closeDB($db);
	return true;
}

function SaveEditRole() {
	$kr_num = $_REQUEST['kr_num'];
	$kr_role = $_REQUEST['kr_role'];

	$db = OpenDB();
	$query = "select kr_role from key_role where kr_num = $kr_num";
	$result = QueryDB($db, $query);
	$numrows = numrowsDB($result);

	if ($numrows != 1) {
	   Error("Schl&uuml;ssel $kr_num ist nicht vorhanden!\n");
	   closeDB($db);
	   return false;
	}

	$query = "update key_role set kr_role = '$kr_role' where kr_num = $kr_num";

	if (!TQueryDB($db, $query)) {
	   Error("SQL-Error: $query");
	   closeDB($db);
	   return false;
	}

	closeDB($db);
	return true;
}

$newrole = $_REQUEST['newrole'];
$editrole = $_REQUEST['editrole'];

if (!isset($editrole)) {
   $editrole = 0;
}

$savenewrole = $_REQUEST['savenewrole'];
$saveeditrole = $_REQUEST['saveeditrole'];

if (isset($savenewrole)) {
   SaveNewRole();
}

if (isset($saveeditrole)) {
   SaveEditRole();
}

?>
<form action="role.php" method="post">
<input type="hidden" name="headline" value="18">
<input type="hidden" name="rstufe" value="<? echo "$rstufe"; ?>">
<input type="hidden" name="menu" value="<? echo "$menu"; ?>">
<input type="hidden" name="unum" value="<? echo "$unum"; ?>">
<button type="submit" name="newrole" value="newrole"><img src="image/button_newrole.png" border=0 alt="Neue Rolle"></button>
<?php
$db = OpenDB();
$query = "select kr_num, kr_role from key_role order by kr_num";
$result = QueryDB($db, $query);

if (!$result) {
   echo "</table></form><br>\n";
   Error("SQL-Error: $query");
   require('footer.inc');
   exit;
}

$numrows = numrowsDB($result);

if ($numrows > 0 || isset($newrole)) {
?>
<table border=1>
   <tr>
      <td class="tbhead">&nbsp;</td>
      <td class="tbhead">Key</td>
      <td class="tbhead">Text</td>
   </tr>
<?php
   $row = 0;

   while ($row < $numrows) {
      $data = fetchDB($result, $row);
      $kr_num = $data[0];
      $kr_role = $data[1];

      if ($kr_num == $editrole) {
         echo "<input type=\"hidden\" name=\"kr_num\" value=\"$kr_num\">\n";
         echo "<tr><td><button type=\"submit\" name=\"saveeditrole\" value=\"$kr_num\"><img src=\"image/new.png\" border=0 alt=\"Edit\"></button></td>";
         echo "<td>$kr_num</td>";
         echo "<td><input type=\"text\" name=\"kr_role\" value=\"$kr_role\" size=40 maxlength=50></td></tr>\n";
      } else {
         echo "<tr><td><button type=\"submit\" name=\"editrole\" value=\"$kr_num\"><img src=\"image/edit.png\" border=0 alt=\"Edit\"></button></td>";
         echo "<td>$kr_num</td><td>$kr_role</td></tr>";
      }

      $row++;
   }

   if (isset($newrole)) {
      echo "<tr><td><button type=\"submit\" name=\"savenewrole\" value=\"savenewrole\"><img src=\"image/new.png\" border=0 alt=\"Neu\"></button></td>";
      echo "<td><input type=\"text\" name=\"kr_num\" size=7 maxlength=7></td>";
      echo "<td><input type=\"text\" name=\"kr_role\" size=40 maxlength=50></td></tr>\n";
   }
?>
</table>
<button type="submit" name="newrole" value="new"><img src="image/button_newrole.png" border=0 alt="Neue Rolle"></button>
</form>
<?php
}

require('footer.inc');
?>

